package org.linlinjava.litemall.db.service;

import org.apache.commons.lang.StringUtils;
import org.linlinjava.litemall.db.config.WxPayScoreConfig;
import org.linlinjava.litemall.db.dao.AgedmallWxNodepositRecordMapper;
import org.linlinjava.litemall.db.domain.AgedmallWxNodepositRecord;
import org.linlinjava.litemall.db.domain.AgedmallWxNodepositRecordExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author houzb
 * @Description
 * @create 2021-12-09 14:34
 */
@Service
public class AgedmallWxNoDepositService {

    @Resource
    private AgedmallWxNodepositRecordMapper agedmallWxNodepositRecordMapper;

    //扣费状态的初始值，表示用户未归还做结算
    private byte NO_DEPOSIT_NO_DEDUCT_INIT = 0;
    //待扣除费用
    private byte NO_DEPOSIT_NO_DEDUCT = 1;
    //已扣除费用
    private byte NO_DEPOSIT_DEDUCTED = 2;
    //扣除费用失败
    private byte NO_DEPOSIT_DEDUCT_FAIL = 3;

    public int recordCreateNoDepositInfo(AgedmallWxNodepositRecord record) {
        //入库记录前，清空由用户取消导致的无用的记录
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(record.getLeaseOrderCode()).andWxStatEqualTo(WxPayScoreConfig.WX_PAY_SCORE_STATE_CREATED);
        agedmallWxNodepositRecordMapper.deleteByExample(recordExample);

        return agedmallWxNodepositRecordMapper.insert(record);
    }

    public int updateNoDepositStat(String outOrderCode, String stat) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(outOrderCode);
        AgedmallWxNodepositRecord record = new AgedmallWxNodepositRecord();
        record.setWxStat(stat);
        record.setLeaseOrderCode(outOrderCode);
        record.setUpdated(LocalDateTime.now());
        return agedmallWxNodepositRecordMapper.updateByExampleSelective(record, recordExample);
    }

    /**
     * 更新免押订单结算后需要扣除的金额
     * @param outOrderCode
     * @param childOrdeerCode
     * @param stat
     * @param payFee
     * @return
     */
    public int updateNoDepositPayFee(String outOrderCode, String childOrdeerCode, String stat, BigDecimal payFee) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(outOrderCode).andChildOrderCodeEqualTo(childOrdeerCode);
        AgedmallWxNodepositRecord record = new AgedmallWxNodepositRecord();
        record.setWxStat(stat);
        record.setLeaseOrderCode(outOrderCode);
        record.setUpdated(LocalDateTime.now());
        record.setPayFee(payFee);
        record.setIsDeduct(NO_DEPOSIT_NO_DEDUCT);
        return agedmallWxNodepositRecordMapper.updateByExampleSelective(record, recordExample);
    }

    /**
     * 免押完成成功更新记录扣款状态
     * @param outOrderCode
     * @return
     */
    public int updateNoDepositDoneSuc(String outOrderCode) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(outOrderCode);
        AgedmallWxNodepositRecord record = new AgedmallWxNodepositRecord();
        record.setIsDeduct(NO_DEPOSIT_DEDUCTED);
        record.setUpdated(LocalDateTime.now());
        return agedmallWxNodepositRecordMapper.updateByExampleSelective(record, recordExample);
    }


    /**
     * 免押完成成功更新记录扣款状态
     * @param outOrderCode
     * @return
     */
    public int updateNoDepositDoneFail(String outOrderCode) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(outOrderCode);
        AgedmallWxNodepositRecord record = new AgedmallWxNodepositRecord();
        record.setIsDeduct(NO_DEPOSIT_DEDUCT_FAIL);
        record.setUpdated(LocalDateTime.now());
        return agedmallWxNodepositRecordMapper.updateByExampleSelective(record, recordExample);
    }

    /**
     * 获取大订单未归还的子订单数量
     * @param bigOrderCode
     * @return
     */
    public long getNoDepositNoSettleCount(String bigOrderCode) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(bigOrderCode).andIsDeductEqualTo(NO_DEPOSIT_NO_DEDUCT_INIT);
        return agedmallWxNodepositRecordMapper.countByExample(recordExample);
    }

    /**
     * 获取已结算的待扣款的费用
     * @param bigOrderCode
     * @return
     */
    public BigDecimal getNoDepositTotalCostFee(String bigOrderCode) {
        AgedmallWxNodepositRecordExample recordExample = new AgedmallWxNodepositRecordExample();
        recordExample.createCriteria().andLeaseOrderCodeEqualTo(bigOrderCode).andIsDeductEqualTo(NO_DEPOSIT_NO_DEDUCT);
        List<AgedmallWxNodepositRecord> recordList = agedmallWxNodepositRecordMapper.selectByExample(recordExample);
        BigDecimal total = BigDecimal.ZERO;
        for(AgedmallWxNodepositRecord record:recordList) {
            total = total.add(record.getPayFee());
        }
        return total;
    }

    /**
     * 根据子订单号获取免押的大订单号
     * @param childOrderCode
     * @return
     */
    public String getOrderIsNoDepositByChildOrderCode(String childOrderCode) {
        if(StringUtils.isBlank(childOrderCode)) {
            return "";
        }
        AgedmallWxNodepositRecordExample nodepositRecordExample = new AgedmallWxNodepositRecordExample();
        nodepositRecordExample.createCriteria().andChildOrderCodeEqualTo(childOrderCode);
        List<AgedmallWxNodepositRecord> wxNodepositRecordList = agedmallWxNodepositRecordMapper.selectByExample(nodepositRecordExample);
        String bigOrderCode = wxNodepositRecordList.size() > 0 ? wxNodepositRecordList.get(0).getLeaseOrderCode() : "";
        return bigOrderCode;
    }

    /**
     * 根据大订单号获取是否为免押订单
     * @param bigOrderCode
     * @return
     */
    public long getOrderIsNoDepositByBigOrderCode(String bigOrderCode) {
        if(StringUtils.isBlank(bigOrderCode)) {
            return 0;
        }
        AgedmallWxNodepositRecordExample nodepositRecordExample = new AgedmallWxNodepositRecordExample();
        nodepositRecordExample.createCriteria().andLeaseOrderCodeEqualTo(bigOrderCode);
        Long count = agedmallWxNodepositRecordMapper.countByExample(nodepositRecordExample);
        return count != null ? count : 0;
    }


}
